package ijij41.togetherTrain.common;

import ijij41.network.*;
import ijij41.togetherTrain.*;
import ijij41.togetherTrain.R.*;
import ijij41.togetherTrain.participate.RoomListActivity;
import ijij41.togetherTrain.resource.*;

import java.util.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;

public abstract class AbstractTrainListActivity extends Activity implements
		Constants {

	private ArrayList<TrainListRow> listSearch;
	private TrainListAdapter trainListAdapter;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.train_list);

		initComponent();
		initContent();
	}

	// TODO layout 참고
	// http://mytalkhome.tistory.com/568

	private void initContent() {

		// get parameters from before activity
		Intent intent = getIntent();

		String date = intent.getStringExtra("date");
		String time = intent.getStringExtra("time");
		String startStation = intent.getStringExtra("startStation");
		String endStation = intent.getStringExtra("endStation");
		String numPerson = intent.getStringExtra("numPerson");

		Log.v("TRAIN", "date : " + date);
		Log.v("TRAIN", "time : " + time);
		Log.v("TRAIN", "startStation : " + startStation);
		Log.v("TRAIN", "endStation : " + endStation);
		Log.v("TRAIN", "numPerson : " + numPerson);

		// get data using above parameters through network.
		ArrayList<TrainListRow> coll = NetworkRequester.getInstance()
				.getTrainList(time, startStation, endStation, numPerson);
		listSearch.addAll(coll);
		trainListAdapter.notifyDataSetChanged();

		// TODO loading thread 필요
		// TODO 조회 자료가 없을 경우에 대한 처리 필요
	}

	private void initComponent() {

		// making list
		listSearch = new ArrayList<TrainListRow>();
		trainListAdapter = new TrainListAdapter(this, R.layout.search_row,
				listSearch);
		ListView list = (ListView) findViewById(R.id.list);
		list.setAdapter(trainListAdapter);
		list.setOnItemClickListener(mItemClickListener);
		// end of making list

		// event handler
		findViewById(R.id.btn_back_img).setOnClickListener(mClickListener);

	}

	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {

			publicOnItemClick(arg0, arg1, position, arg3);

		}

	};

	private Button.OnClickListener mClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btn_back_img:
				finish();
				break;

			default:
				break;
			}

		}
	};

	private class TrainListAdapter extends ArrayAdapter<TrainListRow> {

		private ArrayList<TrainListRow> items;

		public TrainListAdapter(Context context, int textViewResourceId,
				ArrayList<TrainListRow> objects) {
			super(context, textViewResourceId, objects);

			items = objects;

		}

		public View getView(int position, View v, ViewGroup parent) {
			// layout에다가 데이터를 담아서 리턴시켜야 하는데, layout을 어떻게 초기화 시키는가?
			// Inflater는 지정한 layout의 객체들을 모두 생성하고 생성된 객체간의 계층구조까지 완성해주는 놈
			LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.train_list_row, null);

			// 보여주어야할 항목이 몇번째 항목이냐에 따라 그에 맞는 데이터 추츨
			TrainListRow u = items.get(position);

			if (u != null) {
				TextView trainNumTv = (TextView) v.findViewById(R.id.train_num);
				TextView startStationTv = (TextView) v
						.findViewById(R.id.start_station);
				TextView startTimeTv = (TextView) v
						.findViewById(R.id.start_time);
				TextView endStationTv = (TextView) v
						.findViewById(R.id.end_station);
				TextView endTimeTv = (TextView) v.findViewById(R.id.end_time);

				trainNumTv.setText(u.getTrainNum());
				startStationTv.setText(u.getStartStation());
				startTimeTv.setText(u.getStartTime());
				endStationTv.setText(u.getEndStation());
				endTimeTv.setText(u.getEndTime());

				/*
				 * if (tv1 != null) tv1.setText(u.getLabel()); if (tv2 != null)
				 * tv2.setText(u.getContent());
				 */
			}
			return v;
		}
	}

	abstract protected void publicOnItemClick(AdapterView<?> arg0, View arg1,
			int position, long arg3);
	
	
	protected void setLabelTitle(String title_text){
		TextView trainNumTv = ((TextView) findViewById(R.id.tv_title));
		trainNumTv.setText(title_text);
	}

	// Log.v("TRAIN", "TrainListActivity : pos " + position);
	//
	// Intent roomListIntent = new Intent(AbstractTrainListActivity.this,
	// RoomListActivity.class);
	//
	// // //// send parameters
	// TrainListRow trainListRow = (TrainListRow) arg0.getAdapter().getItem(
	// position);
	//
	// Intent intent = getIntent();
	// String date = intent.getStringExtra("date");
	//
	// roomListIntent.putExtra("date", date);
	// roomListIntent.putExtra("trainNum", trainListRow.getTrainNum());
	// roomListIntent.putExtra("startTime", trainListRow.getStartTime());
	// roomListIntent.putExtra("startStation", trainListRow.getStartStation());
	// roomListIntent.putExtra("endtTime", trainListRow.getEndTime());
	// roomListIntent.putExtra("endStation", trainListRow.getEndStation());
	// // ///////////////////
	//
	// startActivity(roomListIntent);
	// }
}
